package com.tank;

import java.awt.*;

/**
 * 子弹
 * @author CaoBaoYin
 * @since 21/8/16 14:50
 */
public class Bullet {

    private static final int SPEED = 10;
    public final Rectangle rectangle;
    private int x;
    private int y;
    private DirectionEnum dir;
    public static final int WIDTH = ResourceManager.bulletU().getWidth();
    public static final int HEIGHT = ResourceManager.bulletU().getHeight();

    private boolean live = true;//判断字段是否存活

    private TankFrame tf;

    private GroupEnum groupEnum = GroupEnum.BAD;

    public Bullet(int x, int y, DirectionEnum dir, GroupEnum groupEnum, TankFrame tf) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.groupEnum = groupEnum;
        this.tf = tf;

        rectangle = new Rectangle(x, y, WIDTH, HEIGHT);
    }

    public GroupEnum getGroupEnum() {
        return groupEnum;
    }

    public void setGroupEnum(GroupEnum groupEnum) {
        this.groupEnum = groupEnum;
    }

    public void paint(Graphics g) {
        // 窗口0,0是左上角，横坐标是X，纵坐标是Y，
        switch (dir) {
            case UP:{
                g.drawImage(ResourceManager.bulletU(),this.x, this.y, null);
                break;
            }
            case DOWN:{
                g.drawImage(ResourceManager.bulletD(),this.x, this.y, null);
                break;
            }
            case LEFT:{
                g.drawImage(ResourceManager.bulletL(),this.x, this.y, null);
                break;
            }
            case RIGHT:{
                g.drawImage(ResourceManager.bulletR(),this.x, this.y, null);
                break;
            }
            default:{

            }
        }

        move();
    }

    private void move() {
        switch (dir) {
            case UP:{
                y -= SPEED;
                break;
            }
            case DOWN:{
                y += SPEED;
                break;
            }
            case LEFT:{
                x -= SPEED;
                break;
            }
            case RIGHT:{
                x += SPEED;
                break;
            }
            default:{

            }
        }

        rectangle.x = this.x;
        rectangle.y = this.y;

        // 子弹消亡
        if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
            this.die();
        }
    }

    public void die() {
        this.live = false;
    }

    public boolean isLive() {
        return live;
    }

    /**
     * 子弹坦克相撞
     * @param tank
     */
    public void collideWith(Tank tank) {
        if (this.groupEnum == tank.getGroupEnum()) return;

        if (this.rectangle.intersects(tank.rectangle)) {
            tank.die();
            this.die();
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public DirectionEnum getDir() {
        return dir;
    }

    public void setDir(DirectionEnum dir) {
        this.dir = dir;
    }

    /*public void setLive(boolean live) {
        this.live = live;
    }*/
}
